package com.bookheaven.core.entity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.search.annotations.Analyze;
import org.hibernate.search.annotations.DateBridge;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.IndexedEmbedded;
import org.hibernate.search.annotations.Resolution;
import org.hibernate.search.annotations.Store;

import entity.base.BaseEntity;

@Entity
@Table(name = "AUTHOR")
@Indexed
public class Author extends BaseEntity {
	
	private static final long	serialVersionUID	= -9055900697991877349L;
	
	@Column(name = "NAME", nullable = false, length = 30)
	@Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
	private String				authorName;
	
	@Column(name = "SURNAME", nullable = false, length = 50)
	@Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
	private String				authorSurname;
	
	@Column(name = "BIRTH_DATE")
	@Field(index = Index.YES, analyze = Analyze.NO, store = Store.NO)
	@DateBridge(resolution = Resolution.DAY)
	@Temporal(TemporalType.DATE)
	private Date				authorBirthDate;
	
	@Column(name = "TITLE", length = 20)
	@Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
	private String				authorTitle;
	
	@Column(name = "ABOUT", length = 255)
	@Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
	private String				authorAbout;
	
	@Column(name = "LINK", length = 255)
	@Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
	private String				authorLink;
	
	@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "authorList")
	@IndexedEmbedded
	@Fetch(FetchMode.SUBSELECT)
	private List<Book>			bookList			= new ArrayList<Book>();
	
	public String getAuthorName() {
		return authorName;
	}
	
	public void setAuthorName(String authorName) {
		this.authorName = authorName;
	}
	
	public String getAuthorSurname() {
		return authorSurname;
	}
	
	public void setAuthorSurname(String authorSurname) {
		this.authorSurname = authorSurname;
	}
	
	public Date getAuthorBirthDate() {
		return authorBirthDate;
	}
	
	public void setAuthorBirthDate(Date authorBirthDate) {
		this.authorBirthDate = authorBirthDate;
	}
	
	public String getAuthorTitle() {
		return authorTitle;
	}
	
	public void setAuthorTitle(String authorTitle) {
		this.authorTitle = authorTitle;
	}
	
	public String getAuthorAbout() {
		return authorAbout;
	}
	
	public void setAuthorAbout(String authorAbout) {
		this.authorAbout = authorAbout;
	}
	
	public String getAuthorLink() {
		return authorLink;
	}
	
	public void setAuthorLink(String authorLink) {
		this.authorLink = authorLink;
	}
	
	public List<Book> getBookList() {
		return bookList;
	}
	
	public void setBookList(List<Book> bookList) {
		this.bookList = bookList;
	}
	
	@Override
	public String toString() {
		return authorName + " " + authorSurname;
	}
	
}
